package com.mbyte.easy.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.qcbyclass.entity.QcbyClass;
import com.mbyte.easy.qcbyclass.service.IQcbyClassService;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.status.entity.Status;
import com.mbyte.easy.status.service.IStatusService;
import com.mbyte.easy.student.entity.Student;
import com.mbyte.easy.student.mapper.StudentMapper;
import com.mbyte.easy.student.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2020-06-16
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private IStudentService studentService;
    @Autowired
    private IQcbyClassService qcbyClassService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IStatusService statusService;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public IPage<Student> getInfopage(Page<Student> userPage, QueryWrapper<Student> queryWrapper) {
        IPage<Student> page = page(userPage,queryWrapper);
        page.setRecords(null);
        List<Student> records = new ArrayList<>();
        SysUser sysUser = Utility.getCurrentUser();
        Long id = sysUser.getId();
        if(id == 1){
            List<Student> student = studentService.list(new LambdaQueryWrapper<Student>());
            for (int i = 0 ; i < student.size();i++) {
                QcbyClass qcbyClass = qcbyClassService.getBaseMapper().selectById(student.get(i).getClassId());
                Status status = statusService.getById(student.get(i).getSex());
                SysUser sysUser1 = userService.selectByPrimaryKey(student.get(i).getUserId());
                student.get(i).setUserName(sysUser1.getUsername());
                student.get(i).setSexTru(sysUser1.getSexType());
                if (qcbyClass != null && status != null) {
                    student.get(i).setClassName(qcbyClass.getClassName());
                    student.get(i).setStatus(status.getStatus());
                }
                records.add(student.get(i));
            }
        }else {
            List<Student> student = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getUserId, id));
            Status status = statusService.getById(student.get(0).getSex());
            QcbyClass qcbyClass = qcbyClassService.getBaseMapper().selectById(student.get(0).getClassId());
            student.get(0).setUserName(sysUser.getUsername());
            student.get(0).setSexTru(sysUser.getSexType());
            if (qcbyClass != null && status != null) {
                student.get(0).setClassName(qcbyClass.getClassName());
                student.get(0).setStatus(status.getStatus());
            }
            records.add(student.get(0));
        }

        page.setRecords(records);
        return page;
    }

    @Override
    public IPage<Student> getAllStuInfopage(Page<Student> userPage, QueryWrapper<Student> queryWrapper,Long id) {
        IPage<Student> page = page(userPage,queryWrapper);
        page.setRecords(null);
        List<Student> records = new ArrayList<>();
        List<Student> studentList = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getClassId , id));
        System.out.println("------------------" + studentList);
        QcbyClass qcbyClass = qcbyClassService.getById(id);
        System.out.println("------------------"+ qcbyClass);
        String className = null;
        if(qcbyClass != null){
            className = qcbyClass.getClassName();
        }
        for (Student student : studentList) {
            SysUser user = userService.selectByPrimaryKey(student.getUserId());
            System.out.println("----------------" + user);
            Status status = statusService.getById(student.getSex());
            student.setSexTru(user.getSexType());
            student.setUserName(user.getUsername());
            if(className != null){
                student.setClassName(className);
            }
            if(status.getStatus() != null){
                student.setStatus(status.getStatus());
            }
            records.add(student);
        }
        page.setRecords(records);
        return page;
    }

}
